<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

class JAJobBoardControllerJACredits extends JAJobBoardController
{
	
	function __construct()
	{
		parent::__construct();
		checkPermission_Administrator('credit'); 
		// Register Extra tasks
		$this->registerTask( 'add',     'edit' );
        $this->registerTask( 'apply',   'save' );        
		$this->registerTask( 'unpublish', 	'publish');
		$this->registerTask( 'default', 	'makedefault');
    }	

	function display( )
	{
	  switch($this->getTask())
		{
			case 'edit'    :
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'layout', 'form'  );
				JRequest::setVar( 'edit', true );
			} break;

			case 'add' :
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'layout', 'form'  );
				JRequest::setVar( 'edit', false );
			} break;
			
		}

		parent::display();
	}

	function save()
	{
		global $option;
		$cids = JRequest::getVar('cid',  0, '', 'array');
		$model = $this->getModel('jacredits');
			
		if ($id = $model->store()) {
			
			if ((isset($cids[0]))&&$cids[0]!=0)
				$msg = JText::_( 'Successfully updated credit' );
			else 
				$msg = JText::_( 'Successfully created credit' );
		} else {
			$msg = JText::_( 'Error found!Fail to save credit' );
		}	
		switch ( $this->_task ) {
			case 'apply':
				$this->setRedirect( "index.php?option=$option&view=jacredits&task=edit&cid[]=$id" , $msg );
				break;

			case 'save':
			default:
				$this->setRedirect( "index.php?option=$option&view=jacredits", $msg );
				break;
		}
	
	}
	
	/**
	 * remove record(s)
	 * @return void
	 */
	function remove()
	{
		global $option;
		
		$model = $this->getModel('jacredits');
		if(!$model->delete()) {
			$msg = JText::_( 'Error found!Fail to delete credit' );
		} else {
			$msg = JText::_( 'Successfully deleted credit(s)' );
		}

		$this->setRedirect( "index.php?option=$option&view=jacredits", $msg );
	}

	/**
	 * cancel editing a record
	 * @return void
	 */
	function cancel()
	{
		global $option;
		
		$msg = JText::_( 'OPERRATION CANCELLED' );
		$this->setRedirect( "index.php?option=$option&view=jacredits", $msg );
	}
	
	/**
	* change Is_Published status
	*/
	/**
	 * publish or unpublish list item 
	 * @return void
	 **/
	function publish(){
		global  $option;
		if($this->getTask()=='unpublish'){
			$publish = 0;
		}
		else{
			$publish = 1;
		}
		
		$model = $this->getModel('jacredits');
				
		if (!$model->dopublish($publish)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
			exit();
		}
		$cache = & JFactory::getCache($option);
		$cache->clean();	
		$this->setRedirect("index.php?option=$option&view=jacredits", "Successfully updated credit!");
	}		
}
?>